import 'package:flutter/cupertino.dart';

typedef RefreshValueFunction<T> = Future<T> Function();

class RefreshValueNotifier<T> extends ValueNotifier<T> {
  final T initialValue;
  final RefreshValueFunction function;

  RefreshValueNotifier(
    this.initialValue,
    this.function,
  ) : super(initialValue) {
    refresh();
  }

  Future<void> refresh({bool cleanData = false, T? setData}) async {
    if (cleanData) {
      value = initialValue;
    }
    if (setData != null) {
      value = setData;
    } else {
      value = await function.call();
    }
    return Future.value();
  }
}
